//
//  HLSpreadPhotoController.m
//  YiCheng
//
//  Created by 常洪亮 on 16/12/8.
//  Copyright © 2016年 yixuanshuo. All rights reserved.
//

#import "HLSpreadPhotoController.h"
#import "UIImageView+WebCache.h"
#define kScreen_Height [UIScreen mainScreen].bounds.size.height
#define kScreen_Width [UIScreen mainScreen].bounds.size.width
@interface HLSpreadPhotoController ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UILabel *_indexLB;
    NSMutableArray *_imagesViewArr;
    NSInteger _count;
}
@property(nonatomic, unsafe_unretained)CGFloat currentScale;
@end

@implementation HLSpreadPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];

    _imagesViewArr = [NSMutableArray arrayWithCapacity:0];
    if (_localImages) {
        _count = _localImages.count;
    
    }else if (_webImages){
        _count = _webImages.count;
    }else{
        _count = 0;
    }
    [self createUI];
    
    // Do any additional setup after loading the view.
}

-(void)createUI{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, kScreen_Width-20, kScreen_Height-kScreen_Height/18-20)];
    [self.view addSubview:_scrollView];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
//    _scrollView.bounces = NO;
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width*(_count+2), _scrollView.frame.size.height);
    
    _scrollView.contentOffset = CGPointMake((_index+1)*_scrollView.frame.size.width, 0);
    
    
    for (int i = 0; i < _count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        [_scrollView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor blackColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
        [imageView addGestureRecognizer:tap];
        if (_localImages) {
            imageView.image = [UIImage imageNamed:_localImages[i]];
        }else if (_webImages){
            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_webImages[i]]]];

        }

        imageView.contentMode = UIViewContentModeScaleAspectFit;
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchImage:)];
        [imageView addGestureRecognizer:pinch];
        [_imagesViewArr addObject:imageView];
    }
    
    UIImageView *firstImageIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
    [_scrollView addSubview:firstImageIV];
    firstImageIV.userInteractionEnabled = YES;
    firstImageIV.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
    [firstImageIV addGestureRecognizer:tap];
    
    if (_localImages) {
        firstImageIV.image = [UIImage imageNamed:_localImages[_count-1]];
    }else if (_webImages){
        [firstImageIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_webImages[_count-1]]]];
    }
    firstImageIV.contentMode = UIViewContentModeScaleAspectFit;
    
    UIImageView *lastImageIV = [[UIImageView alloc] initWithFrame:CGRectMake((_count+1)*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
    [_scrollView addSubview:lastImageIV];
    lastImageIV.userInteractionEnabled = YES;
    lastImageIV.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tap0 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
    [lastImageIV addGestureRecognizer:tap0];

    if (_localImages) {
        lastImageIV.image = [UIImage imageNamed:_localImages[0]];
    }else if (_webImages){
        [lastImageIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_webImages[0]]]];
    }
    lastImageIV.contentMode = UIViewContentModeScaleAspectFit;
    
    _indexLB = [[UILabel alloc] initWithFrame:CGRectMake(0, _scrollView.frame.origin.y+_scrollView.frame.size.height, _scrollView.frame.size.width, kScreen_Height/18)];
    [self.view addSubview:_indexLB];
    _indexLB.textAlignment = NSTextAlignmentCenter;
    _indexLB.backgroundColor = [UIColor blackColor];
    _indexLB.font = [UIFont systemFontOfSize:14];
    _indexLB.textColor = [UIColor whiteColor];
    _indexLB.text = [NSString stringWithFormat:@"%ld / %ld",_index+1,_count];
    
}

-(void)pinchImage:(UIPinchGestureRecognizer *)paramSender{
    if (paramSender.state == UIGestureRecognizerStateEnded) {
        self.currentScale = paramSender.scale;
    }else if(paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){
        paramSender.scale = self.currentScale;
    }
    if (paramSender.scale !=NAN && paramSender.scale != 0.0) {
        paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, paramSender.scale);
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    DLog(@"..");
    _index = (_scrollView.contentOffset.x/(_scrollView.frame.size.width))-1;
    
    
    if (_scrollView.contentOffset.x > _scrollView.frame.size.width*(_count)) {
        _index = 0;
        _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, _scrollView.contentOffset.y);
    }
    if (_scrollView.contentOffset.x < _scrollView.frame.size.width) {
        _index = _count-1;
        _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width*(_count), _scrollView.contentOffset.y);
    }
    for (UIImageView *imageView in _imagesViewArr) {
        imageView.transform = CGAffineTransformMakeScale(1, 1);
    }
    _indexLB.text = [NSString stringWithFormat:@"%ld / %ld",_index+1,_count];
}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
